package util;

import java.awt.Font;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * A cache of the dynamically loaded fonts.
 */
public class FontLoader {

    private static Map<String, Font> cache = new HashMap<String, Font>();
    public static final String OPENSANS_REGULAR = "OpenSans-Regular.ttf";
    public static final String OPENSANS_LIGHT = "OpenSans-Light.ttf";
    public static final String OPENSANS_BOLD = "OpenSans-Bold.ttf";
    
    /**
     * Get a font by a particular name, relative to the res/fonts/ folder
     * @param name Font name
     * @return Font
     */
    public static Font getFont(String name) {
        Font font;

        if ((font = cache.get(name)) != null) {
            return font;
        }

        String fName = "./fonts/" + name;
        try {
            InputStream is = ClassLoader.getSystemResourceAsStream(fName);
            font = Font.createFont(Font.TRUETYPE_FONT, is);
            cache.put(name, font);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.err.println(fName + " not loaded.  Using serif font.");
            font = new Font("serif", Font.PLAIN, 24);
        }
        return font;
    }
}